package appfight.icon.manager;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class IconManager {
	
	private Activity _context;
	final private String IconPath = "/sdcard/cache/appfight/notIcon/";
	
	public IconManager(Activity context)
	{
		_context = context;
	}
	
	public class PInfo {
	    public String appname = "";
	    public String pname = "";
	    public String versionName = "";
	    public int versionCode = 0;
	    public Drawable icon;
	    public void prettyPrint() {
	        Log.d("gameloop",appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
	    }
	}
	
	public class IconQueryResult
	{
		public List<String> Names;
		public List<String> Paths;
		
		public IconQueryResult()
		{
			Names = new ArrayList<String>();
			Paths = new ArrayList<String>();
		}
	}
	
	public IconQueryResult getAllIconsPath()
	{
		IconQueryResult result = new IconQueryResult();
		
		
		try
		{
		
		File f = new File(IconPath);
		for(File iconFile : f.listFiles())
		{
			if(iconFile.isFile())
			{
				try
				{
					String testFormat = iconFile.getAbsoluteFile().toString().toLowerCase();
					
					if(testFormat.endsWith(".png"))
					{
					
					String [] strArray = iconFile.getName().split("\\.");
					String Name = strArray[strArray.length-2];	

					result.Names.add(Name);
					result.Paths.add(iconFile.getAbsolutePath());
					}
				}
				catch(Exception e)
				{
					Log.d("test","test1");
					continue;
				}
			}
		}		
		
		}
		catch(Exception e)
		{
			
		}
		
		return result;		
	}
	
	public void saveIcons(ArrayList<PInfo> appList)
	{
		File targetDirectory;
		targetDirectory = new File(IconPath);
		// have the object build the directory structure, if needed.
		
		targetDirectory.mkdirs();

		
		for(int i=0;i<appList.size();i++)
		{
		Bitmap bitmap = ((BitmapDrawable)appList.get(i).icon).getBitmap(); 
		
		try {
	        File _file = new File(IconPath+appList.get(i).appname+".png");
		    FileOutputStream out  = new FileOutputStream(_file);
		    bitmap = reSizeBitmap(bitmap);
		    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
		    out.close();
		
		        	
		 } catch (Exception e) {
		        e.printStackTrace();
		 } 
		
		}
	}
	
	private Bitmap reSizeBitmap(Bitmap target)
	{
		int width = target.getWidth();
		int height = target.getHeight();
		
		float scaleWidth = ((float)32)/width;
		float scaleHeight = ((float)32)/height;
		
		Matrix matrix = new Matrix();
		
		matrix.postScale(scaleWidth,scaleHeight);
		
		Bitmap resizeBitmap = Bitmap.createBitmap(target,0,0,width,height,matrix,false);
		
		return resizeBitmap;
	}
	
	private ArrayList<PInfo> getPackages() {
	    ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
	    final int max = apps.size();
	    for (int i=0; i<max; i++) {
	        apps.get(i).prettyPrint();
	    }
	    return apps;
	}

	public ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
	    ArrayList<PInfo> res = new ArrayList<PInfo>();        
	    
	    List<PackageInfo> packs = _context.getPackageManager().getInstalledPackages(0);
	    
	    for(int i=0;i<packs.size();i++) {
	        PackageInfo p = packs.get(i);
	        if ((!getSysPackages) && (p.versionName == null)) {
	            continue ;
	        }
	        PInfo newInfo = new PInfo();
	        newInfo.appname = p.applicationInfo.loadLabel(_context.getPackageManager()).toString();
	        newInfo.pname = p.packageName;
	        newInfo.versionName = p.versionName;
	        newInfo.versionCode = p.versionCode;
	        newInfo.icon = p.applicationInfo.loadIcon(_context.getPackageManager());
	        res.add(newInfo);
	    }
	    return res; 
	}

}
